package com.xsh.controller;

import com.github.pagehelper.PageInfo;
import com.xsh.pojo.Menu;
import com.xsh.service.MenuService;
import com.xsh.service.PermissionService;
import com.xsh.utils.GetClassUtils;
import com.xsh.utils.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/menu")
@CrossOrigin("http://localhost:8082")
public class MenuController {

    private MenuService menuService = (MenuService) GetClassUtils.getClass("menuServiceImpl");

    private PermissionService permissionService = (PermissionService) GetClassUtils.getClass("permissionServiceImpl");

    @GetMapping("/user")
    @ResponseBody
    public R user(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        Set<Menu> menuList = menuService.findAllMenu(username);
        Set<String> permissionSet = permissionService.findByUsername(username);
        return R.ok().put("menuList", menuList).put("permissions", permissionSet).put("code", 0);
    }

    @GetMapping("/select")
    @ResponseBody
    public R selectAll(){
        List<Menu> menuList = menuService.findAll();
        return R.ok().put("menuList", menuList);
    }

    @GetMapping("/list")
    @ResponseBody
    public R list(String order, Integer limit, Integer offset){
        PageInfo<Menu> pageInfo = menuService.findByPageInfo(order, limit, offset);
        return R.ok().put("total", pageInfo.getTotal()).put("rows", pageInfo.getList());
    }
}
